#include <iostream>
using std::cout;
using std::endl;

int g_number = 100;

//func1的返回类型是int的引用，执行return语句时，不会发生复制
int & func1(){
    cout << "func1 g_number:" << g_number << endl;
    return g_number;//发生了隐式的绑定
    /* int & ref = g_number; */
    /* return ref; */
}

//func2的返回类型是int型，执行return语句时，
//会复制产生一个临时变量
//执行这个函数时会用一下，当这个函数执行完毕时这个临时变量
//就被销毁了，无法对其取值
int func2(){
    cout << "func2 g_number:" << g_number << endl;
    return g_number;
}

#if 0
int & func3(){
    int l_number = 10;
    return l_number;//隐式绑定
}
#endif

int & func4(){
    int * p = new int(1);
    cout << "heap number:" << *p << endl;
    return *p;
}

void test0(){
    cout << func1() << endl;
    /* cout << func2() << endl; */
    int & ref = func1();
    ref = 200;

    cout << &ref << endl;
    cout << &func1() << endl;
    cout << &g_number << endl;
    cout << g_number << endl;
    /* &func2(); */

    //段错误
    /* cout << func3() << endl; */
}
#if 0
void test1(){
    int * p = new int(4);
    int * p2 = p;
    cout << *p << endl;
    cout << *p2 << endl;
    cout << endl;
    cout << &p << endl;
    cout << &p2 << endl;
}
#endif

void test2(){
    /* cout << func4() << endl;; */
    /* delete &func4(); */

    int & ref = func4();
    delete &ref;

    int a = 3,b = 4;
    cout << a + func4() + b << endl;
}

int main(void){
    /* test0(); */
    /* test1(); */
    test2();
    return 0;
}
